<?php

/*
 * Fresns Control Panel (Standalone)
 * Copyright (C) 2021-Present 刘鑫
 * Released under the MIT License.
 */

namespace App\Plugins\FcpApi\TweetNoticeMessages;

// 配置
use App\Base\Config\BaseConfig;
use App\Http\Config\AssetFileConfig;
use App\Plugins\FcpApi\TweetFiles\TweetFilesConfig;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;

class FsConfig extends BaseConfig
{
    // 主表
    const CFG_TABLE = 'notify_messages';

    // 主表额外搜索字段
    const ADDED_SEARCHABLE_FIELDS = [

    ];
    const STATUS_OPTION = [
        ['key' => 1, 'text' => '未读'],
        ['key' => 2, 'text' => '已读'],
    ];
    const SOURCE_TYPE = [
        ['key' => 1, 'text' => '系统'],
        ['key' => 2, 'text' => '关注'],
        ['key' => 3, 'text' => '点赞'],
        ['key' => 4, 'text' => '评论'],
        ['key' => 5, 'text' => '提及(艾特)'],
    ];
    const SOURCE_CLASS = [
        ['key' => 1, 'text' => '帖子'],
        ['key' => 2, 'text' => '评论'],
    ];
    const POST_OPTION = [
        ['key' => 1, 'text' => '帖子1'],
        ['key' => 2, 'text' => '帖子2'],
    ];
    const COMMENT_OPTION = [
        ['key' => 1, 'text' => '评论1'],
        ['key' => 2, 'text' => '评论2'],
    ];
    const PUSH_CHANNEL = [
        ['key' => 1, 'value'=>1, 'name' => '站内', 'title' => '站内'],
        ['key' => 2, 'value'=>2, 'name' => '邮件',  'text' => '邮件'],
        ['key' => 3, 'value'=>3, 'name' => '短信',  'text' => '短信'],
        ['key' => 4, 'value'=>4, 'name' => '手机',  'text' => '手机'],
        ['key' => 5, 'value'=>5, 'name' => '微信',  'text' => '微信'],
    ];
    // model 使用 - 表单映射
    const FORM_FIELDS_MAP = [
        'id'        => 'id',
        'name'      => 'name',
        'rank_num'  => 'rank_num',
        'is_enable' => 'is_enable',
        'remark'    => 'remark',
        'template_id' => 'template_id',
        'user_id' => 'user_id',
        'member_id' => 'member_id',
        'source_type' => 'source_type',
        'source_class' => 'source_class',
        'source_id' => 'source_id',
        'source_mid' => 'source_mid',
        'status' => 'status',
        'source_brief' => 'source_brief',
        'push_channel' => 'push_channel',
    ];

    // 新增规则
    public function storeRule()
    {
        $table = self::CFG_TABLE;
        $assetFileTable = TweetFilesConfig::CFG_TABLE;

        $rule = [
            'name'          => [
                'required',
                Rule::unique($table)->where(function ($query) {
                    $query->where('deleted_at', null);
                }),
            ],
            'nickname'      => [
                'filled',
                Rule::unique($table)->where(function ($query) {
                    $query->where('deleted_at', null);
                }),
            ],
            'rank_num'  => 'numeric',
            'file_id'   => Rule::exists($assetFileTable, 'id')->where(function ($query) {
                $query->where('deleted_at', null);
            }),
            'file_url'  => 'url',
            'more_json' => 'json',
        ];

        return $rule;
    }

    // 更新规则
    public function updateRule()
    {
        $id = request()->input('id');
        $table = self::CFG_TABLE;
        $assetFileTable = TweetFilesConfig::CFG_TABLE;

        $rule = [
            'id'    => [
                'required',
                Rule::exists($table)->where(function ($query) {
                    $query->where('deleted_at', null);
                }),
            ],
            'name'          => [
                'filled',
                Rule::unique($table)->ignore($id)->where(function ($query) {
                    $query->where('deleted_at', null);
                }),
            ],
            'nickname'          => [
                'filled',
                Rule::unique($table)->ignore($id)->where(function ($query) {
                    $query->where('deleted_at', null);
                }),
            ],
            'rank_num'      => 'numeric',
            'file_id'   => Rule::exists($assetFileTable, 'id')->where(function ($query) {
                $query->where('deleted_at', null);
            }),
            'file_url'  => 'url',
            'more_json' => 'json',
        ];

        return $rule;
    }
}
